set(CMAKE_SYSTEM_NAME Generic)       #利用cmake进行交叉编译必须设置的,如果要创建运行在没有操作系统的嵌入式环境，该参数要设置成Generic
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
cmake_minimum_required(VERSION 3.20)

# 工具链设置
set(TOOLPATH  /home/nx666/local/gcc-arm/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) #生成compile_commands.json

if (WIN32)
	MESSAGE(STATUS "Now is windows!")
    set(CMAKE_C_COMPILER ${TOOLPATH}gcc.exe)
    set(CMAKE_CXX_COMPILER ${TOOLPATH}g++.exe)
    set(CMAKE_ASM_COMPILER ${TOOLPATH}gcc.exe)
    set(CMAKE_AR ${TOOLPATH}ar.exe)
    set(CMAKE_OBJCOPY ${TOOLPATH}objcopy.exe)
    set(CMAKE_OBJDUMP ${TOOLPATH}objdump.exe)
    set(SIZE ${TOOLPATH}size.exe)
elseif (UNIX)
	MESSAGE(STATUS "Now is UNIX-like OS!")
    set(CMAKE_C_COMPILER ${TOOLPATH}gcc)
    set(CMAKE_CXX_COMPILER ${TOOLPATH}g++)
    set(CMAKE_ASM_COMPILER ${TOOLPATH}gcc)
    set(CMAKE_AR ${TOOLPATH}ar)
    set(CMAKE_OBJCOPY ${TOOLPATH}objcopy)
    set(CMAKE_OBJDUMP ${TOOLPATH}objdump)
    set(SIZE ${TOOLPATH}size)
else ()
    MESSAGE(STATUS "Unsupported system!")
endif ()

# 项目设置
project(ch32f203-nx-foc LANGUAGES "C" "CXX" "ASM") #指定语言为c、c++和汇编，如果不设置LANGUAGES，则默认为c、c++

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD 99)

# 编译参数 一般不用改
add_compile_options(-mcpu=cortex-m3 -mthumb)
add_compile_options(-fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common)

#调试信息
add_compile_options(-g -gdwarf-2)

# 编译等级
add_compile_options(-O0)

# 编译信息等级
add_compile_options(-Wall)

# 头文件路径
include_directories(APP
                    Core
                    Debug 
                    Peripheral/inc
                    APP/FreeRTOS/include
                    APP/FreeRTOS/portable/GCC/ARM_CM3
                    APP/foc/inc
                    APP/IQmath
                    APP/modbus/inc
                    APP/motor/inc
                    APP/common/inc
                    APP/interact/inc)

# 宏定义
#add_definitions(-DDEBUG=1)

# 源码文件
file(GLOB_RECURSE SOURCES 
                "APP/*.c"
                "Core/core_cm3.c" 
                "Debug/debug.c"
                "Peripheral/src/*.c" 
                "Startup/startup_ch32f20x_D6.S"
                "APP/FreeRTOS/*.c"
                "APP/FreeRTOS/portable/MemMang/heap_4.c"
                "APP/foc/*.c"
                "APP/modbus/*.c"
                "APP/motor/*.c"
                "APP/common/*.c"
                "APP/interact/*.c"
                )

# 链接参数
set(LINKER_SCRIPT  ${CMAKE_SOURCE_DIR}/Ld/Link.ld)
#自定义链接库路径
link_directories(APP/IQmath/)
#自定义链接库
set(LINKER_LIBS -lIQmathCm3)
#链接参数
add_link_options(-mcpu=cortex-m3
                --specs=nano.specs  #使用静态库 libc_nano.a
                --specs=nosys.specs  #使用静态库 libnosys.a
                -Wl,-u_printf_float
                -Wl,--gc-sections
                -Wl,--cref
                -Wl,--print-memory-usage
                -Wl,-Map,${PROJECT_NAME}.map 
                 )
add_link_options(-T ${LINKER_SCRIPT} ${LINKER_LIBS})

# 编译可执行文件
add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})

# 链接静态库
target_link_libraries(${PROJECT_NAME}.elf ${LINKER_LIBS})

# 输出hex和bin
set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)
set(LST_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.lst)
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD #TARGET后接目标，POST_BUILD表示生成目标后执行COMMAND定义的命令
        COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMAND ${CMAKE_OBJDUMP} --all-headers --demangle --disassemble $<TARGET_FILE:${PROJECT_NAME}.elf> > ${LST_FILE}
        COMMAND ${SIZE} --format=berkeley $<TARGET_FILE:${PROJECT_NAME}.elf>
)
